﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Data.GameData;
using Data.GameData.Utils;
using Data.GameData.Interfaces;

namespace GameFramework.Orders
{
    public class BuildUnitOrder : Order
    {
        private Fleet fleet = null;
        private Unit unitToBuild = null;
        private Unit shipyard = null;

        public BuildUnitOrder()
        {
            Type = OrderType.BUILD;
        }

        public Fleet DestinatinoFleet
        {
            get { return fleet; }
            set { fleet = value; }
        }

        public Unit UnitToBuild
        {
            get { return unitToBuild; }
            set { unitToBuild = value; }
        }

        public Unit ShipYard
        {
            get { return shipyard; }
            set { shipyard = value; }
        }

        /// <summary>
        /// 
        /// </summary>
        public override void Fulfill()
        {
            if (Game.ActiveGame.TurnNumber < TurnComplete)
            {
                Status = OrderStatusType.IN_PROGRESS;
                return;
            }

            Race race = Race.Races.First(r => r.Id == shipyard.OwnerId);

            if (fleet == null)
            {
                fleet = new Fleet { Name = shipyard.Name + " Build Fleet", MapPosition = shipyard.MapPosition, OwnerId = shipyard.OwnerId };
                race.AddFleet(fleet);
            }

            for (int q = 0; q < Quantity; ++q)
            {
                Unit bShip = unitToBuild.Clone() as Unit;
                bShip.MapPosition = shipyard.MapPosition;
                bShip.OwnerId = race.Id;

                fleet.AddFleetUnit(bShip as IFleetElement);
            }

            Status = OrderStatusType.COMPLETE;
        }
    }
}
